<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>xeokit Example</title>
    <link href="../css/pageStyle.css" rel="stylesheet"/>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.13.0/js/all.min.js"></script>
</head>
<body>
<input type="checkbox" id="info-button"/>
<label for="info-button" class="info-button"><i class="far fa-3x fa-question-circle"></i></label>
<canvas id="myCanvas"></canvas>
<div class="slideout-sidebar">
    <img class="info-icon" src="../../assets/images/performance_icon.png"/>
    <h1>Performance optimization</h1>
    <h2>Only enable SAO and edges while camera is not moving</h2
    <br>
    <ul>
        <li>
            <div id="time">Loading JavaScript modules...</div>
        </li>
        <li>
            <a href="../../docs/class/src/viewer/Viewer.js~Viewer.html"
               target="_other">Viewer</a>
        </li>
        <li>
            <a href="../../docs/class/src/plugins/XKTLoaderPlugin/XKTLoaderPlugin.js~XKTLoaderPlugin.html"
               target="_other">XKTLoaderPlugin</a>
        </li>
        <li>
            <a href="http://openifcmodel.cs.auckland.ac.nz/Model/Details/301"
               target="_other">Model source</a>
        </li>
        <li>
            <a href="https://github.com/xeokit/xeokit-sdk/wiki/Importing-Models" target="_other">User Guide</a>
        </li>
    </ul>
</div>
</body>
<script type="module">

    //------------------------------------------------------------------------------------------------------------------
    // Import the modules we need for this example
    //------------------------------------------------------------------------------------------------------------------

    import {Viewer, XKTLoaderPlugin} from "../../dist/xeokit-sdk.min.es.js";

    //------------------------------------------------------------------------------------------------------------------
    // Create a Viewer, arrange the camera
    //------------------------------------------------------------------------------------------------------------------

    const myViewer = new Viewer({
        canvasId: "myCanvas",
        transparent: true
    });

    const cameraControl = myViewer.cameraControl;
    const scene = myViewer.scene;
    const cameraFlight = myViewer.cameraFlight;
    const sao = scene.sao;
    const camera = scene.camera;

    sao.enabled = true;

    cameraControl.followPointer = true;

    camera.eye = [10.45, 17.38, -98.31];
    camera.look = [43.09, 0.5, -26.76];
    camera.up = [0.06, 0.96, 0.16];
    camera.perspective.near = 0.1;
    camera.perspective.far = 5000.0;
    camera.ortho.near = 0.1;
    camera.ortho.far = 5000.0;
    camera.projection = "perspective";

    //------------------------------------------------------------------------------------------------------------------
    // Create an XKT loader plugin, load a model
    //------------------------------------------------------------------------------------------------------------------

    const xktLoader = new XKTLoaderPlugin(myViewer);

    var t0 = performance.now();

    document.getElementById("time").innerHTML = "Loading model...";

    const sceneModel = xktLoader.load({
        id: "myModel",
        src: "../../assets/models/xkt/v8/ifc/OTCConferenceCenter.ifc.xkt",
        edges: true,
        saoEnabled: true,
    });

    sceneModel.on("loaded", () => {
        var t1 = performance.now();
        document.getElementById("time").innerHTML = "Model loaded in " + Math.floor(t1 - t0) / 1000.0 + " seconds<br>Objects: " + sceneModel.numEntities;
    });

    //------------------------------------------------------------------------------------------------------------------
    // Only enable SAO and normal edge emphasis while camera is not moving
    //------------------------------------------------------------------------------------------------------------------

    let timeoutDuration = 1000; // Milliseconds
    let timer = timeoutDuration;
    let saoAndEdgesDisabled = false;

    myViewer.scene.camera.on("matrix", () => {
        timer = timeoutDuration;
        if (!saoAndEdgesDisabled) {
            myViewer.scene.sao.enabled = false;
            myViewer.scene.edgeMaterial.edges = false;
            saoAndEdgesDisabled = true;
        }
    });

    scene.on("tick", (tickEvent) => {  // Milliseconds
        if (!saoAndEdgesDisabled) {
            return;
        }
        timer -= tickEvent.deltaTime;
        if (timer <= 0) {
            if (saoAndEdgesDisabled) {
                myViewer.scene.sao.enabled = true;
                myViewer.scene.edgeMaterial.edges = true;
                saoAndEdgesDisabled = false;
            }
        }
    });

</script>
</html>